package com.epalmpay.controller.channel.poster;

import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.Member;
import com.epalmpay.entity.Poster;
import com.epalmpay.service.group.IGroupService;
import com.epalmpay.service.member.IMemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/27.
 */
@Controller
@RequestMapping(value="/api/poster")
public class PosterController extends BaseController {

    @Resource
    private IGroupService groupService;

    @Resource
    private IMemberService memberService;

    @RequestMapping(value="/savePoster")
    @ResponseBody
    public Map savePoster(HttpServletRequest request) {
        Map result = new HashMap();
        String picaddress = request.getParameter("picaddress");
        String picaddressqr = request.getParameter("picaddressqr");
        String picaddressgz = request.getParameter("picaddressgz");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String wxcardicon = request.getParameter("wxcardicon");
        String wxcardtitle = request.getParameter("wxcardtitle");
        String wxcardcontent = request.getParameter("wxcardcontent");
        int groupid = Integer.parseInt(request.getParameter("groupid"));

        Poster poster = new Poster();
        poster.setGroupid(groupid);
        if(!"".equals(picaddress)) {
            poster.setPicaddress(picaddress);
        }
        if(!"".equals(picaddressqr)) {
            poster.setPicaddressqr(picaddressqr);
        }
        if(!"".equals(picaddressgz)) {
            poster.setPicaddressgz(picaddressgz);
        }
        if(!"".equals(title)) {
            poster.setSpreadtitle(title);
        }
        if(!"".equals(content)) {
            poster.setSpreadcontent(content);
        }
        if(!"".equals(wxcardicon)) {
            poster.setWxcardicon(wxcardicon);
        }
        if(!"".equals(wxcardtitle)) {
            poster.setWxcardtitle(wxcardtitle);
        }
        if(!"".equals(wxcardcontent)) {
            poster.setWxcardcontent(wxcardcontent);
        }
        Poster param = groupService.selectPosterByGroupid(poster);
        if(param != null) {//如果已存在记录则进行修改
            groupService.updateGroupPoster(poster);
        } else {//不存在记录则新增
            groupService.insertGroupPoster(poster);
        }
        result.put("code", "0000");
        return result;
    }

    @RequestMapping(value="/getPoster")
    @ResponseBody
    public Map getPoster(HttpServletRequest request) {
        Map result = new HashMap();

        int groupid = Integer.parseInt(request.getParameter("groupid"));

        Poster poster = new Poster();
        poster.setGroupid(groupid);
        Poster param = groupService.selectPosterByGroupid(poster);
        result.put("code", "0000");
        result.put("data", param);
        return result;
    }

    @RequestMapping(value="/getPosterByMemberid")
    @ResponseBody
    public Map getPosterByMemberid(HttpServletRequest request) throws Exception {
        Map result = new HashMap();

        long memberid = Long.parseLong(request.getParameter("memberId"));
        Member member = new Member();
        member.setId(memberid);
        Member m = memberService.findById(member);

        Poster poster = new Poster();
        poster.setGroupid(m.getGroupId().intValue());
        Poster param = groupService.selectPosterByGroupid(poster);
        result.put("code", "0000");
        result.put("data", param);
        return result;
    }
}
